// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// h [author] rhyme_lph
// L [email]  rhymelph@gmail.com
// t [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/my_invitations_entity.dart';
import 'package:finance_app/src/provider/login/pending_invitation_provider.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/login/invitation_item.dart';
import 'package:finance_app/src/widgets/login/login_top_screen_widget.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "pendingInvitationPage")
class PendingInvitationPage extends StatefulWidget {
  @override
  _PendingInvitationPageState createState() => _PendingInvitationPageState();
}

class _PendingInvitationPageState extends State<PendingInvitationPage>
    with PagePresenter<PendingInvitationPage, PendingInvitationProvider> {
  @override
  Widget get buildWidget => Scaffold(
        body: ListView(
          padding: EdgeInsets.zero,
          children: [
            _buildImageScreen(),
            AppSpaces.h35,
            _buildTipText(
              tip: S.current.you_are_invited_to_join,
            ),
            AppSpaces.h20,
            _buildSecondSelect(),
            AppSpaces.h4,
            NormalButton(
              title: S.current.next,
              onTap: provider.onNext,
            ),
            AppSpaces.h26,
          ],
        ),
      );

  Widget _buildSecondSelect() {
    return listBuilder<List<MyInvitationsEntity>>(
        builder: (BuildContext context, List<MyInvitationsEntity> value,
                Widget child) =>
            Column(
              children: List.generate(
                  Config.myInvitations?.length ?? 0,
                  (index) => InvitationItem(
                        onTap: () {
                          provider.onChangeSelect(Config.myInvitations[index]);
                        },
                        isSelect: value.contains(Config.myInvitations[index]),
                        title: '${Config.myInvitations[index].companyName}',
                        subTitle:
                            '${S.current.invitation} ${Config.myInvitations[index].expiredAt}',
                        icon: Config.myInvitations[index].logo,
                      )),
            ),
        selector: (_, __) => List<MyInvitationsEntity>.from(__.selectList));
  }

  Widget _buildImageScreen() => LoginTopScreenWidget(
        imagePath: A.assetsImagesGraphicPendingInvitation,
        imageName: S.current.pending_invitation,
        imageBottom: 32,
      );

  Center _buildTipText({String tip}) {
    return Center(
      child: Text(
        tip,
        style: TextStyle(
          color: aceBlackColor,
          fontSize: getSp(18),
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}
